package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.*;
import com.guigu.pojo.*;
import com.guigu.service.SupplierSupplyOfGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierSupplyOfGoodsServiceimpl extends ServiceImpl<SupplierSupplyOfGoodsMapper,SupplierSupplyOfGoods> implements SupplierSupplyOfGoodsService {
    @Autowired
    UsersMapper userMapper;//用户mapper
    @Autowired
    ShopInfoMapper shopInfoMapper;//商品表
    @Autowired
    SupplierSupplyOfGoodsMapper supplierSupplyOfGoodsMapper;//供应商维护商品表
    @Autowired
    SupplierGoodsCategoryMapper supplierGoodsCategoryMapper;//供应商维护商品分类表
    @Autowired
    ShopTypeInfoMapper shopTypeInfoMapper;//商品分类表
    @Autowired
    PurchaseInfoMapper purchaseInfoMapper;//采购表

    //根据当前用户，查询供应商目前可以添加到供应商维护商品表的商品
    @Override
    public com.guigu.service.utils.Page Can_be_added_to_supplier_list_data(ShopInfo shopInfo, Integer id,Integer pageno,Integer pagesize) {
        //定义商品集合，最终筛选出来的数据放到这里
        List<ShopInfo> records=new ArrayList();
        //根据用户id，查询供应商维护商品分类表数据
        QueryWrapper queryWrapper=new QueryWrapper<SupplierGoodsCategory>();
        queryWrapper.eq("p_id",id);
        //获取供应商维护分类表数据
        List<SupplierGoodsCategory> supplierGoodsCategoryList=supplierGoodsCategoryMapper.selectList(queryWrapper);
        //查询商品表数据，根据分类筛选商品
        queryWrapper=new QueryWrapper<ShopInfo>();
        //循环设置条件
        for(int i=0;i<supplierGoodsCategoryList.size();i++){
            queryWrapper.eq("shop_type",supplierGoodsCategoryList.get(i).getSortId());
            //检查是否循环到最后一个元素
            if(i+1!=supplierGoodsCategoryList.size()){
                //不是最后一个元素，添加or
                queryWrapper.or();
            }
        }
        //条件查询
        //商品名称
        if(!StringUtils.isEmpty(shopInfo.getShopName())){
            queryWrapper.eq("shop_name",shopInfo.getShopName());
        }
        List<ShopInfo> shopInfoList=shopInfoMapper.selectList(queryWrapper);

        //将筛选出来的商品，循环查询，检查供应商维护商品表内是否存在该商品，如果不存在，就将该商品取出来，添加到最终集合内
        for(ShopInfo s : shopInfoList){
            queryWrapper=new QueryWrapper<SupplierSupplyOfGoods>();
            queryWrapper.eq("g_id",s.getId());
            //根据商品id，查询数据条数，用于证明没有供应过该商品
            if(supplierSupplyOfGoodsMapper.selectList(queryWrapper).size()<1){
                //没有数据，添加到最终集合内
                records.add(s);
            }
            //补全商品类型属性
            if(s.getShopType()!=null){
                s.setType(shopTypeInfoMapper.selectById(s.getShopType()));
            }
        }
        com.guigu.service.utils.Page page=new com.guigu.service.utils.Page();
        return page.pageList(records,pageno,pagesize);
    }


    //根据商品维护表id查询商品和供应商
    @Override
    public SupplierSupplyOfGoods queryById(Integer supplyOrderId) {
        SupplierSupplyOfGoods supply = this.getById(supplyOrderId);
        supply.setUser(userMapper.selectById(supply.getPId()));
        supply.setShop(shopInfoMapper.selectById(supply.getGId()));
        return supply;
    }

    //获取所有的维护商品表数据
    @Override
    public Page<SupplierSupplyOfGoods> queryAllSupplier(SupplierSupplyOfGoods supplier,Integer pageno,Integer pagesize) {
        QueryWrapper<SupplierSupplyOfGoods> queryWrapper=
                new QueryWrapper<SupplierSupplyOfGoods>();
        queryWrapper.eq("is_delete",0);
        queryWrapper.eq("is_check",1);
        if (supplier.getGId()!=null){
            queryWrapper.eq("g_id",supplier.getGId());
        }
        Page<SupplierSupplyOfGoods> page = this.page(new Page<SupplierSupplyOfGoods>(pageno, pagesize),queryWrapper);
        for (SupplierSupplyOfGoods supply : page.getRecords()) {
            //设置用户
            supply.setUser(userMapper.selectById(supply.getPId()));
        }
        return page;
    }

    //添加商品到供应商维护商品表
    @Override
    public Map add(SupplierSupplyOfGoods supplierSupplyOfGoods) {
        Map<String,Object> map=new HashMap<>();
        System.out.println("++++++++++"+supplierSupplyOfGoods);
        //设置属性
        //设置为等待审核
        supplierSupplyOfGoods.setIsCheck(0);
        //设置为正常供应商品
        supplierSupplyOfGoods.setIsDelete(0);
        //进行添加
        if(supplierSupplyOfGoodsMapper.insert(supplierSupplyOfGoods)>0){
            map.put("msg","申请提交成功，等待审核");
            map.put("x",true);
        }else{
            map.put("msg","操作失败！");
            map.put("x",false);
        }
        return map;
    }

    //分页查询
    @Override
    public Page<SupplierSupplyOfGoods> queryAllpage(SupplierSupplyOfGoods supplierSupplyOfGoods, Integer pageno, Integer pagesize) {
        QueryWrapper<SupplierSupplyOfGoods> queryWrapper=new QueryWrapper<SupplierSupplyOfGoods>();
        //条件查询
        //审核状态
        if (supplierSupplyOfGoods.getIsCheck()!=null){
            queryWrapper.eq("is_check",supplierSupplyOfGoods.getIsCheck());
        }
        //删除状态
        if (supplierSupplyOfGoods.getIsDelete()!=null){
            queryWrapper.eq("is_delete",supplierSupplyOfGoods.getIsDelete());
        }
        //检查是否要对数据排序
        if(StringUtils.isNotBlank(supplierSupplyOfGoods.getPriceSorted())){
            if(supplierSupplyOfGoods.getPriceSorted()=="desc"){
                //价格排序为降序
                queryWrapper.orderByDesc("supplier_price");
            }else {
                //没有设置为降序，默认设置为升序
                queryWrapper.orderByAsc("supplier_price");
            }
        }
        Page<SupplierSupplyOfGoods> page = this.page(new Page<SupplierSupplyOfGoods>(pageno, pagesize), queryWrapper);
        //循环集合，补全属性
        for(SupplierSupplyOfGoods supply : page.getRecords()){
            //设置供应商对象
            supply.setUser(userMapper.selectById(supply.getPId()));
            //设置商品对象和商品对象类型
            ShopInfo shopInfo2=shopInfoMapper.selectById(supply.getGId());
            shopInfo2.setType(shopTypeInfoMapper.selectById(shopInfo2.getShopType()));
            supply.setShop(shopInfo2);
        }
        return page;
    }

    //审核供应商维护商品表
    @Override
    public Map checkSupplierSupplyOfGoods(SupplierSupplyOfGoods supplierSupplyOfGoods) {
        Map<String,Object> map=new HashMap<>();
        //检查本次操作是否是拒绝通过
        if(supplierSupplyOfGoods.getIsCheck()==2){
            //将删除状态设置为删除
            supplierSupplyOfGoods.setIsDelete(1);
            //进行修改
            if(supplierSupplyOfGoodsMapper.updateById(supplierSupplyOfGoods)>0){
                //拒绝通过
                map.put("msg","拒绝通过！");
                map.put("x",true);
            }else{
                map.put("msg","操作失败！");
                map.put("x",false);
            }
        }else {
            //不是拒绝通过，不修改删除状态属性
            //进行修改
            if(supplierSupplyOfGoodsMapper.updateById(supplierSupplyOfGoods)>0){
                //允许通过
                map.put("msg","允许通过！！");
                map.put("x",true);
            }else{
                map.put("msg","操作失败！");
                map.put("x",false);
            }
        }
        return map;
    }

    //处理总店采购申请【供应商确认发货】
    @Override
    public Map supplierDelivery(PurchaseInfo purchaseInfo) {
        Map<String,Object> map=new HashMap<>();
        //进行修改
        if(purchaseInfoMapper.updateById(purchaseInfo)>0){
            //修改成功
            map.put("msg","确认发货成功！");
            map.put("x",true);
        }else{
            map.put("msg","操作失败！");
            map.put("x",false);
        }
        return map;
    }

    @Override
    public Page supplierofgoods(SupplierSupplyOfGoods supplierSupplyOfGoods, Integer loginUserId, Integer pageno, Integer pagesize) {
        QueryWrapper queryWrapper=new QueryWrapper();
        Page<SupplierSupplyOfGoods> page=null;
        if(loginUserId!=null){
         queryWrapper.eq("p_id",loginUserId);
         queryWrapper.eq("is_check","1");
         if(supplierSupplyOfGoods.getIsDelete()!=null){
             queryWrapper.eq("is_delete","0");
         }
         page=this.page(new Page<SupplierSupplyOfGoods>(pageno,pagesize),queryWrapper);
         for (SupplierSupplyOfGoods s: page.getRecords()){
             ShopInfo shop=shopInfoMapper.selectById(s.getGId());
             shop.setType(shopTypeInfoMapper.selectById(shop.getShopType()));
             s.setShop(shop);
             if(s.getIsDelete()==0){
                 s.setIsDeleteBoolean(true);
             }else {
                 s.setIsDeleteBoolean(false);
             }
         }
        }
        return page;
    }

    @Override
    public SupplierSupplyOfGoods querybyid(Integer id) {
        SupplierSupplyOfGoods s= this.getById(id);
        if(s!=null){
            ShopInfo shop=shopInfoMapper.selectById(s.getGId());
            shop.setType(shopTypeInfoMapper.selectById(shop.getShopType()));
            s.setShop(shop);
        }
        return s;
    }

    @Override
    public Map updatebysupplierPrice(SupplierSupplyOfGoods supplierSupplyOfGoods) {
        Map map=new HashMap();
        map.put("code","0");
        map.put("msg","编辑失败");
        if(supplierSupplyOfGoods.getSupplierPrice()!=null){
            boolean b=this.updateById(supplierSupplyOfGoods);
            if(b){
                map.put("code","1");
                map.put("msg","编辑成功");
            }
        }
        return map;
    }
    //供应商修改是否上架商品
    @Override
    public Map updatebyisDelete(SupplierSupplyOfGoods supplierSupplyOfGoods) {
        Map<String,Object> map=new HashMap<>();
        if(supplierSupplyOfGoodsMapper.updateById(supplierSupplyOfGoods)>0){
            if(supplierSupplyOfGoods.getIsDelete()==0){
                map.put("msg","启用该商品的供应");
                map.put("x",true);
            }else {
                map.put("msg","关闭该商品的供应");
                map.put("x",false);
            }
        }
        return map;
    }

    //商户主页 显示的供应的商品数量  和 正在供应商品的数量 和 停止供应商品的数量
    @Override
    public List<SupplierSupplyOfGoods> queryAllgys(SupplierSupplyOfGoods supplierSupplyOfGoods) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(supplierSupplyOfGoods.getPId()!=null){
            queryWrapper.eq("p_id",supplierSupplyOfGoods.getPId());
        }
        if(supplierSupplyOfGoods.getIsDelete()!=null){
            queryWrapper.eq("is_delete",supplierSupplyOfGoods.getIsDelete());
        }
        queryWrapper.eq("is_check",1);
        return supplierSupplyOfGoodsMapper.selectList(queryWrapper);
    }
    //商户主页 显示的供应的商品数量  和 正在供应商品的数量 和 停止供应商品的数量
    @Override
    public List<SupplierSupplyOfGoods> queryAllgys(SupplierSupplyOfGoods supplierSupplyOfGoods) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(supplierSupplyOfGoods.getPId()!=null){
            queryWrapper.eq("p_id",supplierSupplyOfGoods.getPId());
        }
        if(supplierSupplyOfGoods.getIsDelete()!=null){
            queryWrapper.eq("is_delete",supplierSupplyOfGoods.getIsDelete());
        }
        queryWrapper.eq("is_check",1);
        return supplierSupplyOfGoodsMapper.selectList(queryWrapper);
    }
}
